﻿using System;
using System.Linq;
using System.Collections.Generic;
using Domain.Core;
using Domain.MainModule.Entities;
using Domain.MainModule.CustomerServices;

namespace Application.MainModule.CustomerServiceManagement
{
    public class CustomerServiceManagementService : ICustomerServiceManagementService, IDisposable
    {
        #region private properties

        private ICustomerServiceRepository CustomerServiceRepository
        {
            get;
            set;
        }

        private ICustomerServiceRepository CustomerServiceTypeRepository
        {
            get;
            set;
        }

        #endregion

        #region constructor

        /// <summary>
        /// Default constructor for CustomerServiceService.
        /// </summary>
        /// <param name="customerServiceRepository">CustomerServiceRepository dependency, usually this is resolved by IoC</param>
        public CustomerServiceManagementService(ICustomerServiceRepository customerServiceRepository)
        {
            if (customerServiceRepository == (ICustomerServiceRepository)null)
            {
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);
            }

            this.CustomerServiceRepository = customerServiceRepository;
        }

        #endregion

        #region ICustomerServiceService implementations

        public void AddCustomerService(CustomerService customerService)
        {
            IUnitOfWork unitOfWork = this.CustomerServiceRepository.UnitOfWork as IUnitOfWork;
            this.CustomerServiceRepository.Add(customerService);
            unitOfWork.Commit();
        }

        public void ChangeCustomerService(CustomerService customerService)
        {
            IUnitOfWork unitOfWork = this.CustomerServiceRepository.UnitOfWork as IUnitOfWork;
            this.CustomerServiceRepository.Modify(customerService);
            unitOfWork.CommitAndRefreshChanges();
        }

        public CustomerService FindCustomerServiceById(Guid customerServiceId)
        {
            CustomerServiceIdSpecification customerServiceIdSpecification = new CustomerServiceIdSpecification(customerServiceId);
            return this.CustomerServiceRepository.FindCustomerService(customerServiceIdSpecification);
        }

        public List<CustomerService> FindCustomerServiceByOrderId(Guid orderId)
        {
            CustomerServiceOrderIdSpecification customerServiceOrderIdSpecification = new CustomerServiceOrderIdSpecification(orderId);
            return this.CustomerServiceRepository.FindCustomerServices(customerServiceOrderIdSpecification).ToList<CustomerService>();
        }

        public void RemoveCustomerService(CustomerService customerService)
        {
            if (customerService == (CustomerService)null)
                throw new ArgumentNullException("customerService");

            IUnitOfWork unitOfWork = this.CustomerServiceRepository.UnitOfWork as IUnitOfWork;
            this.CustomerServiceRepository.Remove(customerService);
            unitOfWork.CommitAndRefreshChanges();

        }

        #endregion

        #region IDisposable implementations

        /// <summary>
        /// Dispose associated context with this domain service.
        /// </summary>
        public void Dispose()
        {
            if (this.CustomerServiceRepository != null
                &&
                this.CustomerServiceRepository.UnitOfWork != null)
            {
                this.CustomerServiceRepository.UnitOfWork.Dispose();
            }
        }

        #endregion
    }
}
